package mybatis.web;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import mybatis.entity.Cart;
import mybatis.entity.Order;
import mybatis.entity.User;
import mybatis.service.AddressService;
import mybatis.service.CartService;
import mybatis.service.impl.AddressServiceImpl;
import mybatis.service.impl.CartServiceImpl;


@WebServlet("/cart/*")
public class CartServlet extends BaseServlet {
	//购物车添加商品
	public void addCart(HttpServletRequest request, HttpServletResponse response) {
		User user =(User) request.getSession().getAttribute("user");
		System.out.println(user);
		int userid = user.getId();
		String gameid = request.getParameter("gid");
		System.out.println(gameid);
		String count = request.getParameter("count");
		CartService service = applicationContext.getBean("cartServiceImpl", CartServiceImpl.class);
		service.add(Integer.parseInt(gameid),userid, count);
	}
	public void getUserCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user =(User) request.getSession().getAttribute("user");
		System.out.println(user);
		int userid = user.getId();
//		int userid = 10;
		CartService service = applicationContext.getBean("cartServiceImpl", CartServiceImpl.class);
		List<Cart> list = service.findByUid(userid);
		System.out.println(list.get(0).getGame().getGame_discount());
		request.setAttribute("list", list);
		System.out.println(list);
		request.getRequestDispatcher("/pages/cart.jsp").forward(request, response);
	}
	//修改单个商品数量
	public void changeNum(HttpServletRequest request, HttpServletResponse response) {
		String change = request.getParameter("change");
		String cartId = request.getParameter("cartId");
		String count = request.getParameter("count");
		CartService service = applicationContext.getBean("cartServiceImpl", CartServiceImpl.class);
		Cart cart = service.findByCid(Integer.parseInt(cartId));
		int gameCount = cart.getGamecount();
		if(change != null) {
			int _gamecount =gameCount+Integer.parseInt(change);
			service.changeCount(Integer.parseInt(cartId), _gamecount);
		}else if(count != null) {
			int _gamecount =Integer.parseInt(count);
			service.changeCount(Integer.parseInt(cartId), _gamecount);
		}
		Cart newCart = service.findByCid(Integer.parseInt(cartId));
		System.out.println(newCart);
		try {
			responseJson(response,newCart);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void getSum(HttpServletRequest request, HttpServletResponse response) throws JsonParseException, JsonMappingException, IOException {
		User user =(User) request.getSession().getAttribute("user");
		System.out.println(user);
		int userid = user.getId();
		String cartlist = request.getParameter("cartlist");
		ObjectMapper om = new ObjectMapper();
//		int userid = 10;
		ArrayList<String> idList = om.readValue(cartlist, ArrayList.class);
		CartService service = applicationContext.getBean("cartServiceImpl", CartServiceImpl.class);
		List<Cart> list = new ArrayList<>();
		for(String i : idList) {
			Cart cart = service.findByCid(Integer.parseInt(i));
			list.add(cart);
		}
		BigDecimal oldAllPrice = new BigDecimal("0");
		BigDecimal newAllPrice = new BigDecimal("0");
		for(Cart cart : list) {
			oldAllPrice = oldAllPrice.add(cart.getOldprice());
			newAllPrice = newAllPrice.add(cart.getFinalprice());			
		}
		Order order = new Order();
		order.setUser(null);
		order.setOrigin_price(oldAllPrice);
		order.setFinal_price(newAllPrice);
		order.setTotalDiscount(oldAllPrice.subtract(newAllPrice));
		System.out.println(order);
		try {
			responseJson(response,order);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	//根据商品id数组删除商品
	//生成订单页面
	public void createOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, String[]> parameterMap = request.getParameterMap();
		String[] cartIds = parameterMap.get("id");
		User user = new User();
		user.setId(10);
		Order order = getOrder(user,cartIds);
		request.setAttribute("order", order);
		request.getSession().setAttribute("order", order);
		request.getRequestDispatcher("/pages/order.jsp").forward(request, response);
	}
	
	private Order getOrder(User user,String[] cartIds) {
		List<Cart> list = new ArrayList<>();
		CartService service = applicationContext.getBean("cartServiceImpl", CartServiceImpl.class);
		for(String i : cartIds) {
			Cart cart = service.findByCid(Integer.parseInt(i));
			list.add(cart);
		}
		BigDecimal oldAllPrice = new BigDecimal("0");
		BigDecimal newAllPrice = new BigDecimal("0");
		for(Cart cart : list) {
			oldAllPrice = oldAllPrice.add(cart.getOldprice());
			newAllPrice = newAllPrice.add(cart.getFinalprice());			
		}
		Order order = new Order();
		order.setUser(user);
		order.setOrigin_price(oldAllPrice);
		order.setFinal_price(newAllPrice);
		order.setTotalDiscount(oldAllPrice.subtract(newAllPrice));
		order.setGamecount(list);
		order.setCount(list.size());
		System.out.println(order);
		return order;
	}
}
